﻿using BySoft.Server.Business;
using BySoft.Server.Common;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using OneSoft.EntityCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.MasteData
{
    public partial class SupplierView : AuthBasePage
    {
        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("SupplierDetailID");
            dt.Columns.Add("SupplierID");
            dt.Columns.Add("ContactName");
            dt.Columns.Add("Tel");
            dt.Columns.Add("Job");
            dt.Columns.Add("Mobile");
            dt.Columns.Add("Email");
            return dt;
        }
        /// <summary>
        /// 联系人明细
        /// </summary>
        public DataTable SupplierDetailDT
        {
            get
            {
                if (Session["SupplierDetailDT"] != null)
                {
                    return (DataTable)Session["SupplierDetailDT"];
                }
                else
                {
                    return CreateDT();
                }
            }
            set
            {
                Session["SupplierDetailDT"] = value;
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindInFo();
            }
        }
        /// <summary>
        /// 绑定页面数据
        /// </summary>
        private void BindInFo()
        {
            string strCode = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id").Trim());
            //获取供应商
            GetSupplier(strCode);
            //获取供应商明细
            GetSupplierdetail(strCode);

        }
        #region 绑定资料
        
        /// <summary>
        /// 获取供应商明细
        /// </summary>
        /// <param name="strCode">供应商ID</param>
        private void GetSupplierdetail(string strCode)
        {
            if (!string.IsNullOrEmpty(strCode))
            {
                DataSet detail = MdSupplierdetail.GetDSByCondition("SupplierID='" + strCode + "'");
                DataTable dt = CreateDT();
                SupplierDetailDT = CommonMethod.CopyDTtoDT(detail.Tables[0], dt);
                BindData();
            }
        }

        /// <summary>
        /// 获取供应商
        /// </summary>
        /// <param name="strCode">供应商ID</param>
        private void GetSupplier(string strCode)
        {
            if (!string.IsNullOrEmpty(strCode))
            {
                MdSupplierEntity supplier = MdSupplier.GetMdSupplierByID(strCode);
                if (supplier != null)
                {
                    this.lblSupplierID.Text = supplier.SupplierID;
                    this.txtAddr.Text = supplier.Addr;
                    this.txtSupplierName.Text = supplier.SupplierName;
                    this.lblSupplierScaler.Text = SysParameter.GetParameterName("SupplierScaler", supplier.SupplierScaler);
                }
            }
        }
        #endregion

        private void BindData()
        {
            if (SupplierDetailDT.Rows.Count > 0)
            {
                this.grdDetail.DataSource = SupplierDetailDT;
                grdDetail.DataBind();
                SetControlAttr(new MdSupplierdetailEntity(), grdDetail);
            }
            else
            {
                UIHelper.BindEmptyGridView(grdDetail, CreateDT());
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }       
    }
}